package com.zisuyi.uilibrary.module.dialog

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.weilele.mvvm.base.helper.appCompatActivity
import com.weilele.mvvm.base.helper.getActivityViewModel
import com.weilele.mvvm.utils.activity.*
import com.weilele.mvvm.utils.fragment.arguments
import com.weilele.mvvm.utils.safeGet
import com.yizisu.library.ad.IAdRewardStatus
import com.zisuyi.uilibrary.R
import com.zisuyi.uilibrary.base.BaseSmallGameActivity
import com.zisuyi.uilibrary.base.SmallGameViewModel
import com.zisuyi.uilibrary.databinding.DialogSmallGameRewardBinding

class SmallGameRewardDialog : BaseTitleDialog<DialogSmallGameRewardBinding>() {
    companion object {
        fun show(
            appCompatActivity: AppCompatActivity?,
            gameName: String,
            text: String,
            reward: Int?
        ) {
            SmallGameRewardDialog().apply {
                arguments {
                    putString("text", text)
                    putInt("reward", reward ?: 0)
                    putString("gameName", gameName)
                }
                show(appCompatActivity)
            }
        }
    }

    private val gameViewModel by lazy { getActivityViewModel<SmallGameViewModel>() }
    private val gameName by lazy { arguments?.getString("gameName") }
    private val reward by lazy { arguments?.getInt("reward") ?: 0 }
    private var isDoubleCoin = false

    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
        isCancelable = false
        setTitle("CONGRATULATIONS")
        arguments?.let {
            mBinding.tvText.textFrom(it.getString("text"))
            mBinding.tvMoney.textFrom(reward.toString())
        }

        setOnCLickCloseListener {
            requestReward()
        }

        mBinding.tvCollect.onClick {
            requestReward()
        }

        mBinding.tvDouble.textFromSpanBean(
            mutableListOf(
                SpanBean("REWARD"),
                SpanBean("  x10", textSize = 20, textColor = getResColor(R.color.colorCoinText)),
            )
        )
        mBinding.flDouble.onClick {
            val isPlaySuccess =
                appCompatActivity?.safeGet<BaseSmallGameActivity<*>>()?.myAd?.showVideoAd{
                    if (it==IAdRewardStatus.Success){
                        isDoubleCoin = true
                        mBinding.tvMoney.textFrom("${reward}x10 = ${reward * 10}")
                        mBinding.flDouble.gone()
                    }
                }
            if (isPlaySuccess != true) {
                getString(R.string.no_ad_hint).toast(Toast.LENGTH_LONG)
            }
        }
    }

    private fun requestReward() {
        gameViewModel.getReward(gameName, isDoubleCoin)
        dismiss()
//        if (!isDoubleCoin){
//            appCompatActivity.safeGet<BaseSmallGameActivity<*>>()?.myAd?.showInterstitial()
//        }
    }
}